package cn.szzsi._admin;

import cn.szzsi.common.bean.AlipayConfig;
import cn.szzsi.common.bean.TenpayConfig;
import cn.szzsi.common.bean.WeixinConfig;
import cn.szzsi.common.kit.CommonUtil;
import cn.szzsi.common.model.PaymentConfig;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 后台类 - 支付方式 2016/9/27.
 */
public class PaymentConfigController extends BaseAdminController<PaymentConfig> {
    private PaymentConfig paymentConfig;
    private TenpayConfig tenpayConfig;
    private AlipayConfig alipayConfig;
    private WeixinConfig weixinConfig;
    private String shopId;

    // 列表
    public void list() {
        shopId=getAttr("shopId");
        findBusinessByPage(shopId,"mall_payment_config");
        render("payment_config_list.html");
    }
    //ajax list
    public void ajaxlist(){
        shopId=getAttr("shopId");
        List<PaymentConfig> lpa = PaymentConfig.dao.find("select * from mall_payment_config where uid = ? order by createDate desc",shopId);
        renderJson(lpa);
    }

    // 是否已存在 ajax验证
    public void checkName() {
        String value = getPara("paymentConfig.name","");
        if (isUnique("paymentconfig", "name", value)) {
            renderText("true");
        } else {
            renderText("false");
        }
    }

    // 添加
    public void add() {
        setAttr("allPaymentConfigType", getAllPaymentConfigType());
        setAttr("allPaymentFeeType", getAllPaymentFeeType());
        setAttr("allTenpayType", getAllTenpayType());
        setAttr("allAlipayType", getAllAlipayType());
        render("payment_config_input.html");
    }

    // 编辑
    public void edit() {
        String id = getPara("id","");
        if (StrKit.notBlank(id)){
            paymentConfig = PaymentConfig.dao.findById(id);
            setAttr("paymentConfig", paymentConfig);
            if (paymentConfig.getPaymentConfigTypeValue() == PaymentConfig.PaymentConfigType.tenpay) {
                setAttr("tenpayConfig", paymentConfig.getConfigObject());
            }else if(paymentConfig.getPaymentConfigTypeValue() == PaymentConfig.PaymentConfigType.alipay) {
                setAttr("alipayConfig", paymentConfig.getConfigObject());
            }else if(paymentConfig.getPaymentConfigTypeValue() == PaymentConfig.PaymentConfigType.weixin) {
                setAttr("weixinConfig", paymentConfig.getConfigObject());
            }
        }
        setAttr("allPaymentConfigType", getAllPaymentConfigType());
        setAttr("allPaymentFeeType", getAllPaymentFeeType());
        setAttr("allTenpayType", getAllTenpayType());
        setAttr("allAlipayType", getAllAlipayType());
        render("payment_config_input.html");
    }


    // 保存
    public void save(){
        paymentConfig = getModel(PaymentConfig.class);
        tenpayConfig = getBean(TenpayConfig.class);//财付通
        alipayConfig = getBean(AlipayConfig.class);//支付宝
        weixinConfig = getBean(WeixinConfig.class);//微信
        shopId=getAttr("shopId");
        String alipayType = getPara("alipayType","");
        String tenpayType = getPara("tenpayType","");

        paymentConfig.set("uid",shopId);
        if (StrKit.notBlank(tenpayType)){
            tenpayConfig.setTenpayType(tenpayType);
        }
        if (StrKit.notBlank(alipayType)){
            alipayConfig.setAlipayType(alipayType);
        }

        String paymentFeeType = getPara("paymentFeeType","");
        if (StrKit.notBlank(paymentFeeType)){
            paymentConfig.setPaymentFeeType(paymentFeeType);
        }
        String paymentConfigType = getPara("paymentConfigType","");
        if (StrKit.notBlank(paymentConfigType)){
            paymentConfig.setPaymentConfigType(paymentConfigType);
        }
        if (paymentConfig.getBigDecimal("paymentFee").compareTo(new BigDecimal("0")) < 0) {
            addActionError("支付手续费金额不允许小于0！");
            return;
        }
        if (paymentConfig.getPaymentConfigTypeValue() == PaymentConfig.PaymentConfigType.tenpay) {
            if (tenpayConfig == null) {
                addActionError("财付通配置不允许为空！");
                return;
            }
            if (tenpayConfig.getTenpayType() == null) {
                addActionError("财付通交易类型不允许为空！");
                return;
            }
            if (StringUtils.isEmpty(tenpayConfig.getBargainorId())) {
                addActionError("财付通商户号不允许为空！");
                return;
            }
            if (StringUtils.isEmpty(tenpayConfig.getKey())) {
                addActionError("财付通密钥不允许为空！");
                return;
            }
            paymentConfig.setConfigObject(tenpayConfig);
        }else if (paymentConfig.getPaymentConfigTypeValue() == PaymentConfig.PaymentConfigType.alipay) {
            if (alipayConfig == null) {
                addActionError("支付宝配置不允许为空！");
                return;
            }
            if (alipayConfig.getAlipayType() == null) {
                addActionError("支付宝交易类型不允许为空！");
                return;
            }
            if (StringUtils.isEmpty(alipayConfig.getBargainorId())) {
                addActionError("支付宝商户号不允许为空！");
                return;
            }
            if (StringUtils.isEmpty(alipayConfig.getKey())) {
                addActionError("支付宝密钥不允许为空！");
                return;
            }
            paymentConfig.setConfigObject(alipayConfig);
        }else if(paymentConfig.getPaymentConfigTypeValue() == PaymentConfig.PaymentConfigType.weixin) {
            if (alipayConfig == null) {
                addActionError("微信配置不允许为空！");
                return;
            }
            if (StringUtils.isEmpty(weixinConfig.getBargainorId())) {
                addActionError("微信支付商户号不允许为空！");
                return;
            }
            if (StringUtils.isEmpty(weixinConfig.getKey())) {
                addActionError("商户支付秘钥不允许为空！");
                return;
            }
            paymentConfig.setConfigObject(weixinConfig);
        }
        saved(paymentConfig);
        renderJson(Ret.ok("info","添加成功!").set("url","list"));
    }

    // 更新
    public void update(){
        paymentConfig = getModel(PaymentConfig.class);
        tenpayConfig = getBean(TenpayConfig.class);
        alipayConfig = getBean(AlipayConfig.class);
        weixinConfig = getBean(WeixinConfig.class);//微信
        String alipayType = getPara("alipayType","");
        String tenpayType = getPara("tenpayType","");

        String paymentFeeType = getPara("paymentFeeType","");
        if (StrKit.notBlank(paymentFeeType)){
            paymentConfig.setPaymentFeeType(paymentFeeType);
        }

        String paymentConfigType = getPara("paymentConfigType","");
        if (StrKit.notBlank(paymentConfigType)){
            paymentConfig.setPaymentConfigType(paymentConfigType);
        }

        if (StrKit.notBlank(tenpayType)){
            tenpayConfig.setTenpayType(tenpayType);
        }
        if (StrKit.notBlank(alipayType)){
            alipayConfig.setAlipayType(alipayType);
        }

        if (paymentConfig.getBigDecimal("paymentFee").compareTo(new BigDecimal("0")) < 0) {
            addActionError("支付手续费金额不允许小于0！");
            return;
        }

        if (paymentConfig.getPaymentConfigTypeValue() == PaymentConfig.PaymentConfigType.tenpay) {
            if (tenpayConfig == null) {
                addActionError("财付通配置不允许为空！");
                return;
            }
            if (tenpayConfig.getTenpayType() == null) {
                addActionError("财付通交易类型不允许为空！");
                return;
            }
            if (StringUtils.isEmpty(tenpayConfig.getBargainorId())) {
                addActionError("财付通商户号不允许为空！");
                return;
            }
            if (StringUtils.isEmpty(tenpayConfig.getKey())) {
                addActionError("财付通密钥不允许为空！");
                return;
            }
            paymentConfig.setConfigObject(tenpayConfig);
        }else if(paymentConfig.getPaymentConfigTypeValue() == PaymentConfig.PaymentConfigType.alipay){
            if (alipayConfig == null) {
                addActionError("支付宝配置不允许为空！");
                return;
            }
            if (alipayConfig.getAlipayType() == null) {
                addActionError("支付宝交易类型不允许为空！");
                return;
            }
            if (StringUtils.isEmpty(alipayConfig.getBargainorId())) {
                addActionError("支付宝商户号不允许为空！");
                return;
            }
            if (StringUtils.isEmpty(alipayConfig.getKey())) {
                addActionError("支付宝密钥不允许为空！");
                return;
            }
            paymentConfig.setConfigObject(alipayConfig);
        }else if(paymentConfig.getPaymentConfigTypeValue() == PaymentConfig.PaymentConfigType.weixin) {
            if (alipayConfig == null) {
                addActionError("微信配置不允许为空！");
                return;
            }
            if (StringUtils.isEmpty(weixinConfig.getBargainorId())) {
                addActionError("微信支付商户号不允许为空！");
                return;
            }
            if (StringUtils.isEmpty(weixinConfig.getKey())) {
                addActionError("商户支付秘钥不允许为空！");
                return;
            }
            paymentConfig.setConfigObject(weixinConfig);
        }
        updated(paymentConfig);
        renderJson(Ret.ok("info","修改成功!").set("url","list"));
        //redirect("/shop/paymentConfig/list");
    }

    // 删除
    public void delete(){
        /*ids = getParaValues("ids");
        Long totalCount = PaymentConfig.dao.getTotalCount();
        if (ids.length >= totalCount) {
            ajaxJsonErrorMessage("删除失败，必须保留至少一个支付方式！");
        }else{
            for (String id : ids) {
                if(PaymentConfig.dao.deleteById(id)){
                    ajaxJsonSuccessMessage("删除成功！");
                }else{
                    ajaxJsonErrorMessage("删除失败！");
                }
            }
        }*/
        String id=getPara("id");
        //long totalCount = DeliveryType.dao.getTotalCount();
        PaymentConfig.dao.deleteById(id);
        renderJson(Ret.ok());

    }

    // 获取所有支付配置类型
    public List<PaymentConfig.PaymentConfigType> getAllPaymentConfigType() {
        List<PaymentConfig.PaymentConfigType> allPaymentConfigType = new ArrayList<PaymentConfig.PaymentConfigType>();
        for (PaymentConfig.PaymentConfigType paymentConfigType : PaymentConfig.PaymentConfigType.values()) {
            //暂时排除掉财付通支付
            if(PaymentConfig.PaymentConfigType.tenpay!=paymentConfigType){
                allPaymentConfigType.add(paymentConfigType);
            }

        }
        return allPaymentConfigType;
    }

    // 获取所有支付手续费类型
    public List<PaymentConfig.PaymentFeeType> getAllPaymentFeeType() {
        List<PaymentConfig.PaymentFeeType> allPaymentFeeType = new ArrayList<PaymentConfig.PaymentFeeType>();
        for (PaymentConfig.PaymentFeeType paymentFeeType : PaymentConfig.PaymentFeeType.values()) {
            allPaymentFeeType.add(paymentFeeType);
        }
        return allPaymentFeeType;
    }

    // 获取所有财付通交易类型
    public List<TenpayConfig.TenpayType> getAllTenpayType() {
        List<TenpayConfig.TenpayType> allTenpayType = new ArrayList<TenpayConfig.TenpayType>();
        for (TenpayConfig.TenpayType tenpayType : TenpayConfig.TenpayType.values()) {
            allTenpayType.add(tenpayType);
        }
        return allTenpayType;
    }

    // 获取所有支付宝交易类型
    public List<AlipayConfig.AlipayType> getAllAlipayType() {
        List<AlipayConfig.AlipayType> allAlipayType = new ArrayList<AlipayConfig.AlipayType>();
        for (AlipayConfig.AlipayType alipayType : AlipayConfig.AlipayType.values()) {
            allAlipayType.add(alipayType);
        }
        return allAlipayType;
    }
}
